package com.nuanshui.heatedloan.credit.pay.validation;

import com.nuanshui.heatedloan.credit.constant.BankCardType;
import com.nuanshui.heatedloan.credit.facade.BankCardService;
import com.nuanshui.heatedloan.credit.form.BankCardForm;
import com.nuanshui.heatedloan.credit.model.BankCard;
import com.nuanshui.heatedloan.credit.pay.PayRequest;
import com.nuanshui.heatedloan.credit.pay.annotation.ValidationAttribute;
import com.nuanshui.heatedloan.credit.pay.constant.PayBehaviorType;
import com.nuanshui.heatedloan.credit.vo.BankCardVO;
import org.springframework.beans.factory.annotation.Autowired;

@ValidationAttribute(group = PayBehaviorType.PrepareOrderBehavior,order = 2)
public class BankCardValidation extends BaseValidation {

    @Autowired
    private BankCardService bankCardService;

    @Override
    public ValidationResult validate(PayRequest request) {

        ValidationResult validationResult= new ValidationResult(ValidationResultCode.VALIDATION_SUCCESS);

        BankCardForm bankCardForm = new BankCardForm();
        bankCardForm.setMerType(1);
        bankCardForm.setCardBankCode(request.getBankCard().getCardNo());
        bankCardForm.setCardBankName(request.getBankCard().getCardBankName());
        BankCardVO bankCardVO =bankCardService.loadBankCardByObject(bankCardForm);

        if(bankCardVO != null && bankCardVO.getStatus() == BankCardType.DISENABLESTATUS){
            validationResult.setValidationResultCode(ValidationResultCode.BANK_VALIDATION_FAIL);
        }
        return validationResult;
    }
}
